<?php
namespace Base\Controller;

use Think\Controller;

class AdminBaseController extends Controller {
	
	public function __construct() {
		hook('admin_begin');
		$admintpl_path = C("SP_ADMIN_TMPL_PATH") . C("SP_ADMIN_DEFAULT_THEME") . "/";
		C("TMPL_ACTION_SUCCESS", $admintpl_path . C("SP_ADMIN_TMPL_ACTION_SUCCESS"));
		C("TMPL_ACTION_ERROR", $admintpl_path . C("SP_ADMIN_TMPL_ACTION_ERROR"));
		parent::__construct();
		$time = time();
		$this->assign("js_debug", APP_DEBUG ? " ? v=$time" : "");
	}
	
	function _initialize() {
		define("TMPL_PATH", C("SP_ADMIN_TMPL_PATH"));
		$this->assign("siteSetting", getSiteSetting());
		
		// 暂时取消后台多语言
		$this->_loadAppAdminMenuLang();
		
		// 验证
		$adminId = session('ADMIN_ID');
		if (empty($adminId)) {
			if (IS_AJAX) {
				$this->error("您还没有登录！", U("Admin/Login/login"));
			} else {
				header("Location:" . U("Admin/Login/login"));
				exit();
			}
		}
		if (!sp_auth_check($adminId)) {
		    $this->error("您没有访问权限！");
		}

		$user = M("User")->where(array('id' => $adminId))->find();
		$this->assign("user", $user);
	}
	
	/**
	 * 消息提示
	 * 
	 * @param type $message        	
	 * @param type $jumpUrl        	
	 * @param type $ajax        	
	 */
	public function success($message = '', $jumpUrl = '', $ajax = false) {
		parent::success($message, $jumpUrl, $ajax);
	}
	
	/**
	 * 模板显示
	 * 
	 * @param type $templateFile
	 *        	指定要调用的模板文件
	 * @param type $charset
	 *        	输出编码
	 * @param type $contentType
	 *        	输出类型
	 * @param string $content
	 *        	输出内容
	 *        	此方法作用在于实现后台模板直接存放在各自项目目录下。例如Admin项目的后台模板，直接存放在Admin/Tpl/目录下
	 */
	public function display($templateFile = '', $charset = '', $contentType = '', $content = '', $prefix = '') {
		parent::display($this->_parseTemplate($templateFile), $charset, $contentType, $content, $prefix);
	}
	
	/**
	 * 获取输出页面内容
	 * 调用内置的模板引擎fetch方法，
	 * 
	 * @access protected
	 * @param string $templateFile
	 *        	指定要调用的模板文件
	 *        	默认为空 由系统自动定位模板文件
	 * @param string $content
	 *        	模板输出内容
	 * @param string $prefix
	 *        	模板缓存前缀*
	 * @return string
	 */
	public function fetch($templateFile = '', $content = '', $prefix = '') {
		$templateFile = empty($content) ? $this->_parseTemplate($templateFile) : '';
		return parent::fetch($templateFile, $content, $prefix);
	}
	
	/**
	 * 自动定位模板文件
	 * 
	 * @access protected
	 * @param string $template
	 *        	模板文件规则
	 * @return string
	 */
	public function _parseTemplate($template = '') {
		$tplPath = C("SP_ADMIN_TMPL_PATH");
		define("SP_TMPL_PATH", $tplPath);
		if ($this->theme) { // 指定模板主题
			$theme = $this->theme;
		} else {
			// 获取当前主题名称
			$theme = C('SP_ADMIN_DEFAULT_THEME');
		}
		
		if (is_file($template)) {
			// 获取当前主题的模版路径
			define('THEME_PATH', $tplPath . $theme . "/");
			return $template;
		}
		$depr = C('TMPL_FILE_DEPR');
		$template = str_replace(':', $depr, $template);
		
		// 获取当前模块
		$module = MODULE_NAME . "/";
		if (strpos($template, '@')) { // 跨模块调用模版文件
			list($module, $template) = explode('@', $template);
		}
		
		$module = $module . "/";
		
		// 获取当前主题的模版路径
		define('THEME_PATH', $tplPath . $theme . "/");
		
		// 分析模板文件规则，如果模板文件名为空，则按照默认规则定位
		if ('' == $template) {
			$template = CONTROLLER_NAME . $depr . ACTION_NAME;
		} elseif (false === strpos($template, '/')) {
			$template = CONTROLLER_NAME . $depr . $template;
		}
		
// 		$cdn_settings = sp_get_option('cdn_settings');
// 		if (!empty($cdn_settings['cdn_static_root'])) {
// 			$cdn_static_root = rtrim($cdn_settings['cdn_static_root'], '/');
// 			C("TMPL_PARSE_STRING.__TMPL__", $cdn_static_root . "/" . THEME_PATH);
// 			C("TMPL_PARSE_STRING.__PUBLIC__", $cdn_static_root . "/public");
// 			C("TMPL_PARSE_STRING.__WEB_ROOT__", $cdn_static_root);
// 		} else {
			C("TMPL_PARSE_STRING.__TMPL__", __ROOT__ . "/" . THEME_PATH);
// 		}
		
		C('SP_VIEW_PATH', $tplPath);
		C('DEFAULT_THEME', $theme);
		define("SP_CURRENT_THEME", $theme);
		
		$file = addTemplateFileSuffix(THEME_PATH . $module . $template);
		$file = str_replace("//", '/', $file);
		if (!file_exists_case($file)) {
			E(L('_TEMPLATE_NOT_EXIST_') . ':' . $file);
		}
		return $file;
	}
	
	/**
	 * 加载后台用户语言包
	 */
	private function _loadAppAdminMenuLang() {
		$defaultLang = C('DEFAULT_LANG');
		$langSet = C('ADMIN_LANG_SWITCH_ON', null, false) ? LANG_SET : $defaultLang;
		$adminMenuLangFile = APP_PATH . MODULE_NAME . "/Lang/" . $langSet . "/admin_menu.php";
		if (is_file($adminMenuLangFile)) {
			$lang = include $adminMenuLangFile;
			L($lang);
		}
	}
}